﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections.ObjectModel;

using Clipper.Model;
using Clipper.Model.ViewModel;
using System.Threading;
using System.ComponentModel;
using Clipper.Model.EnumModel;
using Clipper.Model.DetectionModel;
using System.Runtime.Serialization.Json;
using System.IO;

namespace Global
{
    /// <summary>
    /// 基本支持类
    /// </summary>
    public partial class ServiceProvider
    {
        #region 常量
        /// <summary>
        /// 导出的Excel数据版本号，用于判断是否与数据库对应
        /// </summary>
        public const string FileVersion = "1";
        /// <summary>
        /// 每个反应条中反应杯数量
        /// </summary>
        public const byte NumberCupsPerStrip = 8;
        /// <summary>
        /// 每个试管架中试管数量
        /// </summary>
        public const byte NumberTubesPerShelf = 10;
        /// <summary>
        /// 初始孵育箱位置
        /// </summary>
        public const byte IncubatorSeat = 2;
        /// <summary>
        /// 一根试管最大测试数
        /// </summary>
        public const int TestTubeTestTimes = 40;
        /// <summary>
        /// 一根试管测同一项目最大次数
        /// </summary>
        public const int TestTubeRepeat = 2;
        /// <summary>
        /// 试管架最大测试数
        /// </summary>
        public const int TestTubeShelfTimes = 400;
        /// <summary>
        /// 最大发光反应数（最大恒温等待数）
        /// </summary>
        public const byte MaxLightSeat = 10;
        /// <summary>
        /// 最大反应杯数
        /// </summary>
        public const int MaxReactionCup = 896;
        /// <summary>
        /// 最大反应条数
        /// </summary>
        public const int MaxReactionStrip = 112;

        const byte _maxIncubatorCount = 50;


        public static bool ISCheckSave = false;
        /// <summary>
        /// 孵育箱最大反应条数量
        /// </summary>
        public static  byte MaxIncubatorCount
        {
            get
            {
                return _maxIncubatorCount;
            }
        }

        const byte _maxWaitReactionCount = 20;
        /// <summary>
        /// 恒温等待最大反应条数量
        /// </summary>
        public static byte MaxWaitReactionCount
        {
            get
            {
                return _maxWaitReactionCount;
            }
        }
        public SampleInfo sampleinfotion;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cup"></param>
        /// <param name="stripID"></param>
        /// <param name="holeID"></param>
        /// <returns></returns>
        public static SampleInfomation getSampleInfomation(ReactionCup cup,int stripID,int holeID)
        {
            SampleInfomation info = new SampleInfomation();
            info.SampleID = (int)cup.SampleInfoID;
            info.HoleID = holeID;
            info.StripID = stripID;
            info.SampleMetering = cup.DetectItemModel.bloodAmount;
            info.Reagent1Metering = cup.DetectItemModel.reagentAmount2;
            //info.Reagent1ID=
            return info;
        }

        public static ReagentInfomation getReagentInfomation(ReactionCup cup,int stripID,int holeID)
        {
            ReagentInfomation reagentinfo = new ReagentInfomation();
            reagentinfo.HoleID = holeID;
            reagentinfo.StripID = stripID;
            //reagentinfo.Reagent2ID = 0;
            reagentinfo.Reagent2Metering = cup.DetectItemModel.reagentAmount2;
            //reagentinfo.Reagent3ID = 0;
            reagentinfo.Reagent3Metering = cup.DetectItemModel.reagentAmount3;

            return reagentinfo;
        }


        /// <summary>
        /// 最大试剂盒孔位数
        /// </summary>
        public const byte MaxKitCount = 20;
        /// <summary>
        /// 试剂安装超时常数
        /// </summary>
        public const int SetReagentTimeOut = 30;

        #endregion
        //公共的类中添加cupStrip类

        //杯子条展示进展
        private static string selectedValues;

        private static double sampleConcentration;//自动定标时输入的定标血浆的浓度
        public static double SampleConcentration
        {
            get
            {
                return sampleConcentration;
            }

            set
            {
                sampleConcentration = value;
            }
        }

        //定义一个结果集合
        public static List<double> calibrationList = new List<double>();
        //暂时存储结果list集合
        public static List<double> shortCalibraList = new List<double>();
        #region 静态变量
        private static string[,] standardResult = new string[12,2]; //标准曲线的结果数组
        public static string[,] StandardResult
        {
            get
            {
                return standardResult;
            }

            set
            {
                standardResult = value;
            }
        }

        public static double pTISIValue;  //ptISIvalue


        #region 检测数据
        static ObservableCollection<ReactionStrip> _gReactionStripList;
        /// <summary>
        /// 反应条列表
        /// </summary>
        public static ObservableCollection<ReactionStrip> gReactionStripList
        {
            get
            {
                return _gReactionStripList;
            }
            set
            {
                _gReactionStripList = value;
            }
        }

        #endregion
        #region 检测数据 用杯子作为反应的链条
        static ObservableCollection<ReactionCup> _gReactionCupList;
        /// <summary>
        /// 反应杯列表
        /// </summary>
        public static ObservableCollection<ReactionCup> gReactionCupList
        {
            get
            {
                if (_gReactionCupList==null)
                {
                    _gReactionCupList = new ObservableCollection<ReactionCup>();
                }
                return _gReactionCupList;
            }
            set
            {
                _gReactionCupList = value;
            }
        }


        #endregion
        #region 臂吸试剂列表
        static ObservableCollection<ReactionCup> _sampleList;
        /// <summary>
        /// 臂吸试剂列表
        /// </summary>
        public static ObservableCollection<ReactionCup> SampleList
        {
            get
            {
                return _sampleList;
            }
            set
            {
                _sampleList = value;
            }
        }
        #endregion
        #region 臂吸试剂列表
        static ObservableCollection<ReactionCup> _reagentList;
        /// <summary>
        /// 臂吸试剂列表
        /// </summary>
        public static ObservableCollection<ReactionCup> ReagentList
        {
            get
            {
                return _reagentList;
            }
            set
            {
                _reagentList = value;
            }
        }
        #endregion

        #region 运行日志
        static ObservableCollection<Thread> _detectThreadList;
        /// <summary>
        /// 检测线程列表
        /// </summary>
        public static ObservableCollection<Thread> DetectThreadList
        {
            get
            {
                return _detectThreadList;
            }
            set
            {
                _detectThreadList = value;
            }
        }
        /// <summary>
        /// 当前的方法
        /// </summary>
        public static string nameCurrentMethod = string.Empty;
        /// <summary>
        /// 当前的类
        /// </summary>
        public static string nameCurrentClass = string.Empty;

        #endregion

        #region 等待处理信息列表
        static ObservableCollection<int> _waitMessage;
        /// <summary>
        /// 等待处理的信息
        /// </summary>
        public static ObservableCollection<int> WaitMessage
        {
            get
            {
                return _waitMessage;
            }
            set
            {
                _waitMessage = value;
            }
        }
        #endregion

        #region 全局控制参数
        /// <summary>
        /// 全局暂停 0没暂停 1暂停进样 2 暂停小车传送 3 全部暂停
        /// </summary>
        public static byte Pause
        {
            get;
            set;
        }
        /// <summary>
        /// 试剂安装超时
        /// </summary>
        public static bool ReagentTimeOut;
      
        /// <summary>
        /// 仪器允许运行
        /// </summary>
        public static bool Performability;
        /// <summary>
        /// 自检通过
        /// </summary>
        public static bool SelfCheckPass=true;

        /// <summary>
        /// 通过管理员密码验证
        /// </summary>
        public static bool AdminValidate=true;
        /// <summary>
        /// 工程师密码验证
        /// </summary>
        public static bool EngineerValidate=true;

        /// <summary>
        /// 是否正常关闭机器
        /// </summary>
        public static bool IsCloseNormal = false;
        #endregion

        #endregion

        #region 变量

        /// <summary>
        /// 数据实体
        /// </summary>
        public object DataModel
        {
            get;
            set;
        }
        /// <summary>
        /// 数据操作的类型
        /// </summary>
        public InputEnum InputName
        {
            get;
            set;
        }
        private static ObservableCollection<CupStrip> cupStrip;
        public static ObservableCollection<CupStrip> CupStrip
        {
            get
            {
                return cupStrip;
            }

            set
            {
                cupStrip = value;
            }
        }

        public static string SelectedValues
        {
            get
            {
                return selectedValues;
            }

            set
            {
                selectedValues = value;
            }
        }

        public static int DoTestCount
        {
            get
            {
                return doTestCount;
            }

            set
            {
                doTestCount = value;
            }
        }

        private static int doTestCount;  //记录做实验的次数


        #endregion

        public ServiceProvider()
        {
            
        }
        public static ServiceProvider CreateProvider()
        {
            ServiceProvider serviceprovider = new ServiceProvider();
            ReagentKitVM = new ReagentKitVM();
            Monitor = new MonitorVM();
            StandardCurveVM = new StandardCurveVM();
            WorkSheetVM = new WorkSheetVM();
            _configVM = new ConfigVM();
            DetectThreadList = new ObservableCollection<Thread>();
            //样本和试剂列表
            SampleList = new ObservableCollection<ReactionCup>();
            ReagentList = new ObservableCollection<ReactionCup>();

            WaitMessage = new ObservableCollection<int>();

            //向集合中添加 状态 赋值
            CupStrip = new ObservableCollection<CupStrip>();

            Algorithmclass = new AlgorithmClass();

            variableClass = new VariableClass();//全局变量类


            return serviceprovider;
        }
        public  ServiceProvider CreateProvider(ServiceProvider service)
        {
            ServiceProvider serviceprovider = new ServiceProvider();
            serviceprovider.DataModel = service.DataModel;
            serviceprovider.InputName = service.InputName;
            return serviceprovider;
        }

        /// <summary>
        /// Json转换成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonText"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(string jsonText)
        {
            DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonText));
            T obj = (T)s.ReadObject(ms);
            ms.Dispose();
            return obj;
        }


    }
}
